<?php
/*
 * @Author: 一品网络技术有限公司
 * @Date: 2022-08-06 08:12:35
 * @LastEditTime: 2022-10-26 20:49:39
 * @FilePath: \web\app\admin\controller\Files.php
 * @Description:
 * 联系QQ:58055648
 * Copyright (c) 2022 by 东海县一品网络技术有限公司, All Rights Reserved.
 */

declare(strict_types=1);

namespace app\admin\controller;

use Curl\Curl;

class Files extends Base
{
    protected $table;
    protected function initialize()
    {
        parent::initialize();
        $this->table = new \app\admin\model\Files;
    }
    public function index($keywords = '',$category_id=0,$ypcms_id=0, $page = 1, $isuse = 1,$ftype="",$tag='')
    {
        $map = [];
        if($ftype){
            $map[]=['ftype','like',"$ftype%"];
        }
        if (!empty($keywords)) {
            $map[] = ['name', 'like', "%{$keywords}%"];
        }
        $map[] = ['ypcms_id', '=', 0];
        if($tag){
            $map[]=['tag','=',$tag];
        }
        if($category_id){
            $category_ids=\app\common\model\Category::whereRaw('find_in_set('.$category_id.',path)')->column('id');
            if($category_ids){
                $map[] = ['category_id', 'in', $category_ids];
            }else{
                $map[] = ['category_id', '=', $category_id];
            }
        }

        if($ypcms_id){
            $map[]=['ypcms_id','in',[0,$ypcms_id]];
        }

        $files_list = $this->table->where($map)->with(['ypcms','category'])->order(['id' => 'DESC'])->append(['mod'])->paginate(30, false, ['page' => $page]);
        $files_list = json_decode(json_encode($files_list),true);
        $files_list['search_type_data'] = [
            [
                'id'=>'image',
                'title'=>'图片'
            ],
            [
                'id'=>'word',
                'title'=>'文档'
            ],
            [
                'id'=>'attachment',
                'title'=>'附件'
            ],
        ];
        return $this->success('附件获取成功', $files_list);
    }

    public function details($id)
    {
        $res=$this->table::find($id);
        return $this->success('获取成功', $res);
    }
    //获取文件列表
    public function getjson($ftype = 0, $page = 1, $keyword = '')
    {
        $map = [];
        if ($ftype) {
            $map[] = ['ftype', 'like', "$ftype%"];
        }
        if (!empty($keyword)) {
            $map[] = ['name', 'like', "%{$keyword}%"];
        }
        //只读取未使用的附件
        $map[] = ['ypcms_id', '=', 0];
        $files_list = $this->table->where($map)->order(['id' => 'DESC'])->paginate(24, false, ['page' => $page]);

        return $files_list;
    }
    //远程保存图片
    public function downloadFile($str='',$category_id=0){
        if($str==''){
            return;
        }
        //https://s.cccgx.com/images/qw/19832/20220507/20220507022641_25040.png


        $fileSize = $this->site['uploadsize'] * 1024 * 1000;
        $fileExt = $this->site['filetype'];
        $filesDir = $this->site['filesdir'];

        $fileType=explode(",",$fileExt);

        $suffix = substr(strrchr($str, '.'), 1);
        if (!in_array($suffix, $fileType))
        {
            return $this->error('系统不支持的文件');
        }
        $dirName = './storage/'.$filesDir.'/'.date("Ymd").'/';

        if (!file_exists($dirName))
        {
            mkdir($dirName, 0777, true);
        }

        $fileName= $dirName.time() . rand(0, 9) . '.' . $suffix;
        $curl = new Curl();
        $curl->get($str);
        if ($curl->error) {
            return $this->error($curl->error_code);
        }
        else {
            $remotefile = file_get_contents($str);
            $fileSize= strlen($remotefile);
            $fileType=explode(" ",$curl->response_headers[2])[1];
            $res = fopen($fileName, 'a');
            fwrite($res, $curl->response);
            fclose($res);
            //附件入库
            $data['name'] = basename($str);
            $data['filepath'] = substr($fileName,1);
            $data['addtime'] = time();
            $data['ftype'] = $fileType;
            $data['fsize'] = $fileSize;
            $data['user_id'] = 0;
            $data['admin_id'] = $this->admin->id;
            $data['ypcms_id'] = 0;
            $data['category_id']=$category_id;
            $data['isq'] = 0;
            $r =  $this->table::create($data);
            return $this->success('获取成功', $r);
        }
        




    }
    public function del($id, $bind = 0)
    {
        if (!$id) {
            return;
        }
        if ($bind == 1) {
            $r = $this->table->save(
                [
                    'ypcms_id' => '',
                    'ypcms_type' => '',
                ],
                ['id' => $id]
            );
            $msg = "取消成功";
        } else {
            $r = $this->table->destroy($id);
            $msg = "删除成功";
        }
        if ($r) {
            return $this->success($msg);
        } else {
            return $this->error('操作失败');
        }
    }

    public function delete($ids)
    {
        if (!$ids) {
            return 0;
        }
        if (is_array($ids)) {
            $ids = array_map('intval', $ids);
        }
        $rs = $this->table::destroy($ids);
        if ($rs) {
            return $this->success("删除成功");
        }
    }
}
